package com.bee.beeAuth.Intecepor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 拦截器，拦截请求传递token
 * @description:
 * @author: chenguosong
 * @date: 2022-12-16 16:21
 */
public class TokenTelayRequestIntecepor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        // 从header获取Authorization
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes srat = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = srat.getRequest();
        String token = request.getHeader("Authorization");
        if (StringUtils.isNotBlank(token)) {
            //将token传递出去
            template.header("Authorization", token);
        }
    }
}
